package com.njxxted.activiti.modules.business.leave.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njxxted.activiti.common.util.PageUtil;
import com.njxxted.activiti.common.util.StringUtils;
import com.njxxted.activiti.modules.business.leave.dao.BusLeaveDao;
import com.njxxted.activiti.modules.business.leave.entity.BusLeaveReqEntity;
import com.njxxted.activiti.modules.business.leave.service.BusLeaveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述：请假流程业务表 service
 * <p>
 * 作者：HuTongFu
 * 邮箱：hu_tongfu@163.com
 * 时间：2019-08-07 14:18:16
 */
@Service("busLeaveService")
public class BusLeaveServiceImpl implements BusLeaveService {

    @Resource
    private BusLeaveDao busLeaveDao;

    @Override
    public BusLeaveReqEntity queryObject(String id) {
        return busLeaveDao.queryObject(id);
    }

    @Override
    public PageUtil<BusLeaveReqEntity> queryList(BusLeaveReqEntity param) {
        if (Objects.isNull(param)) {
            return null;
        }
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<BusLeaveReqEntity> busLeaveEntityList = busLeaveDao.queryList(param);
        PageInfo<BusLeaveReqEntity> pageInfo = new PageInfo<>(busLeaveEntityList);
        return new PageUtil<>(busLeaveEntityList, (int) pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum());

    }

    @Override
    public List<BusLeaveReqEntity> queryListByBean(BusLeaveReqEntity param) {
        return busLeaveDao.queryListByBean(param);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String save(BusLeaveReqEntity busLeaveEntity) {
        if (busLeaveEntity.getId() != null) {
             busLeaveDao.update(busLeaveEntity);
        } else {
            busLeaveEntity.setId(StringUtils.uuidGenerator());
             busLeaveDao.save(busLeaveEntity);
        }
        return busLeaveEntity.getId();
    }

    @Override
    public int update(BusLeaveReqEntity busLeaveEntity) {
        return busLeaveDao.update(busLeaveEntity);
    }

    @Override
    public int delete(String id) {
        return busLeaveDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return busLeaveDao.deleteBatch(ids);
    }

}
